package com.pig4cloud.pig.mt.handler;

import com.pig4cloud.pig.mt.entity.WxMsg;
import com.pig4cloud.pig.mt.service.WxMsgService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class LogHandler extends AbstractHandler {
    @Autowired
	WxMsgService wxMsgService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) {
        try {
            log.info("接收到请求消息，内容：{}", wxMessage.getOpenId()/*JsonUtils.toJson(wxMessage)*/);
            wxMsgService.addWxMsg(new WxMsg(wxMessage));
        } catch (Exception e) {
            log.error("记录消息异常", e);
        }
        return null;
    }

}
